package org.ssssssss.example.scheduled;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Configuration
public class JobRunner implements CommandLineRunner {

    private final Logger logger = LoggerFactory.getLogger(JobRunner.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;

    @Override
    public void run(String... args) {
        String sql = "select * from magic_job where status=1 and ifnull(del_flag,0)=0";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : list) {
            SchedulingRunnable task = new SchedulingRunnable(map.get("invoke_mode").toString(),
                    map.get("method_type") != null ? map.get("method_type").toString() : null,
                    map.get("path").toString(),
                    map.get("params").toString());
            cronTaskRegistrar.addCronTask(task, map.get("cron_expression").toString());
        }
        logger.info("定时任务启动成功");
    }
}
